/**
 * 
 */
require(['jquery',
         'global',
         'module/util',
         'bootbox.min',
         'module/ajax',
         'jquery.validate',
         'bootstrap',
         'pixeladmin',
         'datatables2',
         'jquery.datatables.bootstrap3',
         'requirejs/domready!'], 
	function($, global, util, bootbox){
	//window.PixelAdmin.start();
	//表单校验加提交
	var mainForm = $('#mainForm').validate({
		onkeyup : false,
		rules:{
			oldPassword:{
				required: true,
            	rangelength: [4,30]
            },
            newPassword:{
				required: true,
		    	rangelength: [4,30]
		    },
		    newConfirmPassword:{
				required: true,
		    	rangelength: [4,30],
		    	equalTo: "#newPassword"
		    },
		    mobile:{
	    		required : true,
	    		rangelength: [11,11],
	    		number : true
	    	}
	    },
	    messages : {
	    	oldPassword:{
	    		  required: "当前密码不能为空",
	        	  rangelength : $.format("当前密码：{0}-{1}个字符")
	        },
	    	newPassword:{
	  		  required: "新密码不能为空",
	      	  rangelength : $.format("新密码长度：{0}-{1}个字符")
		    },
		    newConfirmPassword:{
		  		  required: "确认密码不能为空",
		      	  rangelength : $.format("新密码长度：{0}-{1}个字符"),
		      	  equalTo: "确认密码要与密码一致"
			},
			mobile : {
	    		required : '手机号码不能为空',
	    		rangelength: $.format("手机号码为11位数字"),
	    		number : '手机号码只能是数字'
	    	}
	    },
		submitHandler: function(form) {
			util.ajax_submit(form).complete(function(xhr) {
				var result = $.parseJSON(xhr.responseText);
				if(result.code=="ACK"){
					$("#mainModal").modal('hide');
					$("#mainModal input[type='text'],#mainModal input[type='password']").val('');
				}
			});
	    },
	    errorPlacement:function(error, element){//自定义错误提示放置的位置
	    	var $errorPanel = '<div class=" validate-error-panel"></div>';
	    	element.closest(".info_item").after($errorPanel);
	    	error.appendTo(element.closest(".info_item").next('.validate-error-panel'));
	    },
	    highlight: function(element, errorClass) {  
	    	$(element).addClass('highlight-error');//未通过验证的元素高亮
        },
        unhighlight: function(element, errorClass) {  
	    	$(element).removeClass('highlight-error');//通过验证去掉高亮
        },
        success: function(label) {  
        }  
	});
	
	//关闭modal框
	$("#mainModal").on('click','.close',function(){
		mainForm.resetForm();
		$("#mainModal").find("input,select").removeClass('highlight-error');
		$("#mainModal").find("input[type='password']").val('');
	});
	
	
	$('body').on('click', '.toEditUserInfo', function(){
		var me = $(this);
		$.ajax({
	        'url' : global.context+"/web/adminuser/toChangePassword/"+me.data('id'),
	        'data':'',
	        'type' : 'get',
	        'dataType' : 'json',
	        'success' : function(user) {
        		console.log(user);
	        	$("#id").val(user.id);
	        	$("#usernameSpan").html(user.username);
	        	$("#nameSpan").html(user.name);
	        	mainForm.resetForm();
	    		$("#mainModal").find("input,select").removeClass('highlight-error');
	    		$("#mainModal").find("input[type='password']").val('');
	        },
	        'error': function(request) {
                $('#alertsDanger').find(".modal-body").text(request.statusText);
				$('#alertsDanger').modal();
            },
		});
	});
	
	$('body').on('click','.changeWorkStatus',function(){
		var target=$(this);
		var id=target.attr("data-id");
		var v = $("#changeWorkStatus");
		$.ajax({
            url: global.context+'/web/adminuser/changeWorkStatus/'+id,
            type: "GET",
            cache:false,
            dataType: "json",
            success: function(result) {
                if(result.code != "NACK"){
                	if(result.data=="暂停工作")
                		v.html("<i class='dropdown-icon fa fa-pause'></i>&nbsp;&nbsp;"+result.data);
                	else
                		v.html("<i class='dropdown-icon fa fa-play'></i>&nbsp;&nbsp;"+result.data);
                }
            }
        });
	});
	
});